package com.yanggeng.demo.beginnerguidedemo.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.activity.DrawerLayoutActivity;
import com.yanggeng.demo.beginnerguidedemo.R;

/**
 * Created by yanggeng on 2017/7/6.
 */

public class MineFragment extends Fragment {

    Toolbar toolbar;
    AppBarLayout appBarLayout;
    TextView tv_title;
    private View toolbarOpen, toolbarClose, bgContent, bgToolbarOpen, bgToolbarClose;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, null);
        toolbar = (Toolbar) view.findViewById(R.id.mine_toolbar);
        appBarLayout = (AppBarLayout) view.findViewById(R.id.mine_app_bar_layout);
        toolbarOpen = view.findViewById(R.id.layout_toolbar_open);
        toolbarClose = view.findViewById(R.id.layout_toolbar_close);
        bgContent = view.findViewById(R.id.bg_open);
        bgToolbarOpen = view.findViewById(R.id.bg_toolbar_open);
        bgToolbarClose = view.findViewById(R.id.bg_toolbar_close);

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                double offset = Math.abs(verticalOffset);
                int scrollRange = appBarLayout.getTotalScrollRange();
                float alpha;
                if (offset <= scrollRange / 2) {
                    alpha = (float) offset / (scrollRange / 2);
                    toolbarOpen.setVisibility(View.VISIBLE);
                    toolbarClose.setVisibility(View.GONE);
                    bgToolbarOpen.setBackgroundColor(Color.argb((int) (255 * alpha), 43, 136, 229));
                    Log.e("MineFragment", "offset = " + offset + ", scrollRange = " + scrollRange + ", alpha = " + alpha);
                } else { //折叠状态
                    alpha = (float) (scrollRange - offset) / (scrollRange / 2);
                    toolbarOpen.setVisibility(View.GONE);
                    toolbarClose.setVisibility(View.VISIBLE);
                    bgToolbarClose.setBackgroundColor(Color.argb((int) (255 * alpha), 43, 136, 229));
                }
                alpha = (float) offset / scrollRange;
                bgContent.setBackgroundColor(Color.argb((int) (255 * alpha), 43, 136, 229));
            }
        });
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
        final DrawerLayout drawerLayout = ((DrawerLayoutActivity) getActivity()).getDrawerLayout();
//        view.findViewById(R.id.mine_nav).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                drawerLayout.openDrawer(GravityCompat.START);
//            }
//        });
//        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar
//                , R.string.navigation_drawer_open
//                , R.string.navigation_drawer_close);
//        drawerLayout.addDrawerListener(toggle);
//        toggle.syncState();
        return view;
    }
}
